let requestMode = require('js/requestsMode.js');
let constants = require('js/constants.js');
let session = require('js/session.js');
let Tools = require('js/Tools.js');
///购物车数量
var buyCarCount = 0;
App({
  onLaunch: function() {
    ////
    let that = this;
    /*页面进入，默认刷新session中的sKey*/
    wx.login({
      success: function (loginResult) {
        wx.getUserInfo({
          success:function(res){
            const header = {
              'code':loginResult.code,
              'iv':res.iv,
              'encrypted-data':res.encryptedData
            };
            session.set('iv',res.iv);
            session.set('encrypted-data',res.encryptedData);
            requestMode.sendRequest('login','POST',{},header).then(function(result){
              if(result.data.code == 200){
                session.set('sKey',result.data.data.sKey);
                  that.getBuyCarNum();
              }})
          }
        });
      }
    });
  },
    ////获取购物车数量
    getBuyCarNum : function () {
        // Tools.requestBuyCarCount(true);
    },
  setSkey:function(){
    wx.login({
      success: function (loginResult) {
        wx.getUserInfo({
          success:function(res){
            const header = {
              'code':loginResult.code,
              'iv':res.iv,
              'encrypted-data':res.encryptedData
            };
            session.set('iv',res.iv);
            session.set('encrypted-data',res.encryptedData);
            requestMode.sendRequest('login','POST',{},header).then(function(result){
              if(result.data.code == 200){
                session.set('sKey',result.data.data.sKey);
                  that.getBuyCarNum();
              }})
          }
        });
      }
    });
  },
  getUserInfo: function(cb) {
    var that = this;
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.getUserInfo({
        withCredentials: false,
        success: function(res) {
          that.globalData.userInfo = res.userInfo
          typeof cb == "function" && cb(that.globalData.userInfo)
        }
      })
    }
  },
  globalData: {
    userInfo: null
  }
});
